<?php

namespace module;

class UploadApi
{
    protected $size;

    protected $ext = 'jpg,png,gif,pdf,doc,docx,xlsx,mp4';

    const IMAGE = 1;
    const PDF = 2;
    const WORD = 3;
    const EXCEL = 4;
    const VIDEO = 5;

    /**
     * 上传基类
     * @param $file
     * @return array|string
     */
    public function upload($file)
    {
        $data = $file->getInfo();
        $saveData = $this->getSaveData($this->getDataType($data['name']));
        $savePath = ROOT_PATH . 'public/index' . DS . 'uploads/'.$saveData;
        $info = $file->validate(['size' => $this->size,'ext' => $this->ext])->move($savePath);
        if($info){
            $truePath = $this->changePath($info->getsaveName());
            return $savePath.'/'.$truePath;
        }else{
            return ['msg'=>$file->getError()];
        }
    }

    /**
     *
     * 检查格式 文件大小
     * @param $type_name
     * @return int
     */
    public function getDataType($type_name)
    {
        $string = explode('.',$type_name);
        $type = end($string);
        $data_type = 0;
        switch ($type){
            case 'jpg':
                $this->size = 999999999999;
                $data_type = self::IMAGE;
                break;
            case 'png':
                $this->size = 999999999999;
                $data_type = self::IMAGE;
                break;
            case 'gif':
                $this->size = 999999999999;
                $data_type = self::IMAGE;
                break;
            case 'pdf':
                $this->size = 999999999999;
                $data_type = self::PDF;
                break;
            case 'doc':
                $this->size = 999999999999;
                $data_type = self::WORD;
                break;
            case 'docx':
                $this->size = 999999999999;
                $data_type = self::WORD;
                break;
            case 'xlsx':
                $this->size = 999999999999;
                $data_type = self::EXCEL;
                break;
            case 'mp4':
                $this->size = 999999999999 * 1204;
                $data_type = self::VIDEO;
                break;
        }
        return $data_type;
    }


    /**
     * 根据类型判断上传文件夹
     * @param $type
     * @return string
     */
    public function getSaveData($type)
    {
        $dataName = '';
        switch ($type){
            case self::IMAGE;
                $dataName = 'image';
                break;
            case self::PDF;
                $dataName = 'pdf';
                break;
            case self::WORD;
                $dataName = 'word';
                break;
            case self::EXCEL;
                $dataName = 'excel';
                break;
            case self::VIDEO;
                $dataName = 'video';
                break;
        }
        return $dataName;
    }

    /**
     * 转换斜杠
     * @param $url
     * @return string
     */
    public function changePath($url)
    {
        list($date,$name) = explode('\\',$url);
        return $date.'/'.$name;
    }
}